#!/usr/bin/env bash

set -e

buildScript="./org_build.sh"

function usage() {
    echo "Usage: $( basename $0 ) [-r | -b | -v | -c | -h ]"
    echo "    -v: 建造类型，可以是{Debug,MinSizeRel,Release,RelWithDebInfo}, 默认Release"
    echo "    -b: build"
    echo "    -r: 打开--use_tensorrt选项"
    echo "    -c: clean"
    echo "    -h: print this message"
    exit 1
}

function build_wheel() {
    # 开始编译
    # --enable_language_interop_ops
    #                       Enable operator implemented in language other than cpp
    # --skip_submodule_sync
    #                       Don't do a 'git submodule update'. Makes the Update
    # --build_wheel         Build Python Wheel.
    # wheel文件: build/Linux/${conf}/dist/onnxruntime-1.15.0-cp310-cp310-linux_x86_64.whl

    if ${useTensorRT}
    then
        optStr="--use_tensorrt"
    else
        optStr=" "
    fi
    
    echo ${buildScript}\
        "--config ${conf}"\
        "--enable_language_interop_ops"\
        "--build_shared_lib"\
        "--build_wheel"\
        "--parallel"\
        "--skip_submodule_sync"\
        "${optStr}"

    sleep 2
    
    ${buildScript} \
        --config ${conf} \
        --enable_language_interop_ops \
        --build_shared_lib \
        --build_wheel \
        --skip_submodule_sync \
        --parallel \
        ${optStr}
}

function build_clean() {
    echo ${buildScript}\
        "--config ${conf}"\
        "--clean"

    sleep 1

    ${buildScript} \
        --config ${conf} \
        --clean

    exit 0
}

build=false
useTensorRT=false
conf='Release'
while getopts "rbv:ch" args
do
    case "${args}" in 
        r)
            useTensorRT=true;;
        b)
            build=true;;
        v)
            conf="$OPTARG"
            ;;
        c)
            build_clean
            ;;
        *)
            usage;;
    esac
done

if ${build}
then
    build_wheel
else
    usage
fi
